from enum import Enum
from typing import Optional, List, Dict, Any, Union
from pydantic import BaseModel
from app.schemas.metrics_schema import Chart, BarChart, ScatterPlot, LineChart
from app.schemas.assets_schema import Text, Ontology, KnowledgeGraph, GraphPattern,GraphRule ,MediaFile,TextRule
import numpy as np


# 1. 任务状态
class TaskStatus(str, Enum):
    PENDING = "PENDING"
    RUNNING = "RUNNING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"


# 2. 产品类型
class ProductType(str, Enum):
    TEXT = "TEXT"
    FILE = "FILE"
    TEXT_RULE = "TEXT_RULE"
    GRAPH_RULE = "GRAPH_RULE"
    ONTOLOGY = "ONTOLOGY"
    KNOWLEDGE_GRAPH = "KNOWLEDGE_GRAPH"

# FacetNet算法的输入参数
class FacetNetInputParams(BaseModel):
    edges_list: List[List[List[int]]]  # 多个时间步的边列表，格式为[[[u1,v1], [u2,v2], ...], [...], ...]
    alpha: float = 0.9  # 算法参数，控制历史信息的影响
    tsteps: int = 10  # 时间步数
    N: int = 128  # 节点数量
    M: int = 4  # 社区数量
    with_truth: bool = False  # 是否提供真实社区信息
    truth_comms: Optional[List[List[List[int]]]] = None  # 真实社区信息，格式为[[[node1,comm1], [node2,comm2], ...], [...], ...]

# FacetNet算法的时间步结果
class TimeStepResult(BaseModel):
    time: int  # 时间步
    communities: List[int]  # 每个节点的社区分配
    soft_modularity: float  # 软模块度
    mutual_info: Optional[float] = None  # 互信息分数（如果提供了真实社区信息）
    community_matrix: Optional[List[List[int]]] = None  # 社区节点之间的关系矩阵

# FacetNet算法的输出参数
class FacetNetOutputParams(BaseModel):
    results: List[TimeStepResult]  # 每个时间步的结果
    algorithm: str = "FacetNet"  # 算法名称
    parameters: Dict[str, Any]  # 算法参数
    community_results: Optional[List[List[int]]] = None  # 每个时间步的节点社区分配 [时间步][节点i] = 社区ID

# 3. 输入参数
class InputParams(BaseModel):
    # 你的代码需要的输入参数
    facetnet_params: Optional[FacetNetInputParams] = None
    pass

# 4. 输出参数
class OutputParams(BaseModel):
    # 算法输出参数
    facetnet_results: Optional[FacetNetOutputParams] = None
    pass

# 5. 算法请求
class AlgorithmRequest(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    input_params: InputParams

# 6. 算法中间响应
class AlgorithmMiddleResponse(BaseModel):
    task_id: str
    task_callback_url: str
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    input_params: InputParams
    error_message: Optional[str] = None

    metrics: List[Chart] = []


# 7. 算法最终响应
class AlgorithmResponse(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    error_message: Optional[str] = None

    output_params: OutputParams

    metrics: List[Chart] = []
